package com.fzg.common.demo.xiachufang;

import com.fzg.common.tool.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URLEncoder;

/**
 * create by fzg
 * 2022/12/21 16:02
 */

@Slf4j
public class GetDateFromXiaChuFang {

    public static void main(String[] args){
        String keyword = "红烧鱼";
        // 网站url
        // String url = "https://www.xiachufang.com/search/?keyword=红烧鱼&cat=1001";
        String headerUrl = "https://www.xiachufang.com";
        try {
            String encode = URLEncoder.encode(keyword, "utf-8");
            String url = "https://www.xiachufang.com/search/?keyword=" + encode + "&cat=1001";
            // Document document = Jsoup.parse(new URL(url), 100000);
            String s = HttpUtils.sendGetRequest(url);
            log.info(s);
            Document document = Jsoup.parse(s);
            Elements list = document.getElementsByClass("normal-recipe-list");
            Element element = list.get(0);
            Elements list1 = element.getElementsByClass("list");
            Element element1 = list1.get(0);
            Elements a = element1.getElementsByTag("a");
            log.info("a标签\n" + a.get(0).toString());
            Element element2 = a.get(0);
            String href = element2.attr("href");
            log.info("地址：" + href);

            // 请求制作详情的页面
            String detailUrl = headerUrl + href;
            log.info(detailUrl);

            String s1 = HttpUtils.sendGetRequest(detailUrl);
            Document document1 = Jsoup.parse(s1);
            Elements elementsByClass = document1.getElementsByClass("page-title");
            Element element3 = elementsByClass.get(0);
            String text = element3.text();
            log.info(text);

            Elements elementsByClass1 = document1.getElementsByClass("block recipe-show");
            Element element4 = elementsByClass1.get(0);
            Elements img = element4.getElementsByTag("img");
            Element element5 = img.get(0);
            String src = element5.attr("src");
            log.info(src);

            String type = document1.getElementsByClass("page-container").get(0).getElementsByTag("li")
                    .get(1).getElementsByTag("a").get(0).getElementsByTag("span")
                    .get(0).text();
            log.info(type);

            Elements number = element4.getElementsByClass("number");
            String text1 = number.get(0).text();
            String text2 = number.get(1).text();
            log.info(text1);
            log.info(text2);

            // 用料
            Elements table = element4.getElementsByTag("table");
            Elements tr = table.get(0).getElementsByTag("tr");
            for (Element element6 : tr) {
                String name = element6.getElementsByClass("name").get(0).text();
                log.info(name);
                String unit = element6.getElementsByClass("unit").get(0).text();
                log.info(unit);
            }

            // 步骤
            Elements elementsByClass2 = element4.getElementsByClass("steps").get(0).getElementsByClass("container");
            for (Element element6 : elementsByClass2) {
                String stepText = element6.getElementsByTag("p").get(0).text();
                log.info(stepText);
                String stepImg = element6.getElementsByTag("img").get(0).attr("src");
                log.info(stepImg);
            }

            // 小贴士（有的有，有的没有）
            if (!element4.getElementsByClass("tip-container").isEmpty()){
                String tip = element4.getElementsByClass("tip-container").get(0).getElementsByClass("tip").get(0).text();
                log.info(tip);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
